/**
 * UserController
 *
 * @description :: Server-side actions for handling incoming requests.
 * @help        :: See https://sailsjs.com/docs/concepts/actions
 */

 const BlockChainApp = require("../services/BlockChainApp");
const PrescriptionContract = require("../services/contracts/PrescriptionContract");
 
 const Message = require("../services/Message");
 
 // const Message = require("../services/Message")
 
 module.exports = {
   // 新增
   createPrescription: async function(req, res){
        var param = req.body.data
        console.log("new record: ", param)
        // var instance = await BlockChainApp.contracts.CaseRecords.deployed()
        PrescriptionContract.addPrescription(param)
        .then(
            data=>{
                return res.json(Message.success("新增处方信息完成"))
            }
        )
        .catch(
            err=>{
                console.log(err)
                return res.json(Message.error("新增处方信息错误"))
            }
        )
    },

    // 查看所有
    getAllPrescription: async function(req, res){
        // var instance = await BlockChainApp.contracts.CaseRecords.deployed()

        PrescriptionContract.getAllPrescrition()
        .then(
            data=>{
                return res.json(Message.success(data))
            }
        )
        .catch(
            err=>{
                console.log(err)
                return res.json(Message.error("获取处方信息错误"))
            }
        )
    },

    // 条件查询
    getPrescriptionWithCondition: async function(req, res){
        var param = req.body.data
        PrescriptionContract.getPrescriptionWithCondition(param)
        .then(data=>{
            return res.json(Message.success(data))
        })
        .catch(error=>{
            console.log(error)
            return res.json(Message.error("获取处方信息错误"))
        })
    },

    // 更新
    updatePrescription: async function(req, res){
        var param = req.body.data
        console.log("更新处方信息", param)
        PrescriptionContract.updatePrescription(param)
        .then(data=>{
            console.log("更新处方信息完成", data)
            return res.json(Message.success(data))
        })
        .catch(error=>{
            console.log("error: ", error)
            return res.json(Message.error("更新处方信息错误"))
        })
    },

 };
 
 